package com.dubito.dao.impl;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.dubito.entity.Dept;
import com.dubito.utils.MyBatisUtil;

public class DeptDaoImpl {

	/**
	 * 用于插入数据到dept表。
	 * @param dept 部门信息
	 * @return 表示受影响的行数
	 */
	@SuppressWarnings("null")
	public int insert(Dept dept){
		
		 /* 1.读取配置信息
		 * 2.构建session工厂
		 * 3.创建session
		 * 4.启动事务（可选）
		 * 5.数据处理
		 * 6.提交事务、回滚事务（可选）
		 * 7.关闭session  
		 */		
		int i=0;
		SqlSession session=null;
		String config="myBatis-config.xml";
       Reader reader = null;
		try {
			reader=Resources.getResourceAsReader(config);
			SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
			session=sqlSessionFactory.openSession();
			//事务默认自动启动
			//SQL映射文件定义的命名空间+SQL语句的ID定位SQL语句,例如下的：cn.it.entity.DeptMapper.insert
			i=session.insert("com.dubito.entity.DeptMapper.insert",dept);
			session.commit();		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
		}finally{
			//关闭reader对象，这里略
			session.close();
		}		
		return i;
	}
	
	/**
	 * @param dept 部门信息
	 * @return 保存信息后受影响的行数
	 */
	public int saveDept(Dept dept) {
		SqlSession session=null;
		int i = 0;
		try {
			session = MyBatisUtil.getSession();
			i = session.insert("com.dubito.entity.DeptMapper.insertDept", dept);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
		} finally {
			try {
				MyBatisUtil.closeSession();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}

		return i;
	}
	
	public int  update(Dept dept){
		SqlSession session = null;
		int i=0;

		try {
			session=MyBatisUtil.getSession();
			//方法的第一个参数为DeptMapper.xml文件的命名空间+id
			 i=session.update("com.dubito.entity.DeptMapper.update",dept);
			//System.out.println("受影响行数："+i);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
			
		}finally{
				MyBatisUtil.closeSession();
		}	
		return i;
	}
	public int  delete(Integer deptId){
		SqlSession session = null;
		int i=0;
		
		try {
			session=MyBatisUtil.getSession();
			//方法的第一个参数为DeptMapper.xml文件的命名空间+id
			i=session.update("com.dubito.entity.DeptMapper.delete",deptId);
			System.out.println("受影响行数："+i);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
			
		}finally{
			MyBatisUtil.closeSession();
		}	
		return i;
	}
	public Dept  select(Integer deptId){
		SqlSession session = null;
		Dept dept = null;
		
		try {
			session=MyBatisUtil.getSession();
			//方法的第一个参数为DeptMapper.xml文件的命名空间+id
			dept = session.selectOne("com.dubito.entity.DeptMapper.select",deptId);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
			
		}finally{
			MyBatisUtil.closeSession();
		}	
		return dept;
	}
	public List<Dept>  selectList(String address){
		SqlSession session = null;
		List<Dept> depts = null;
		
		try {
			session=MyBatisUtil.getSession();
			//方法的第一个参数为DeptMapper.xml文件的命名空间+id
			depts = session.selectList("com.dubito.entity.DeptMapper.selectList",address);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
			
		}finally{
			MyBatisUtil.closeSession();
		}	
		return depts;
	}
	
	public List<Dept>  selectListIf(Dept dept){
		SqlSession session = null;
		List<Dept> depts = null;
		
		try {
			session=MyBatisUtil.getSession();
			//方法的第一个参数为DeptMapper.xml文件的命名空间+id
			depts = session.selectList("com.dubito.entity.DeptMapper.selectListIf",dept);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
			
		}finally{
			MyBatisUtil.closeSession();
		}	
		return depts;
	}
	
	public List<Dept>  selectListWhere(Dept dept){
		SqlSession session = null;
		List<Dept> depts = null;
		
		try {
			session=MyBatisUtil.getSession();
			//方法的第一个参数为DeptMapper.xml文件的命名空间+id
			depts = session.selectList("com.dubito.entity.DeptMapper.selectListWhere",dept);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
			
		}finally{
			MyBatisUtil.closeSession();
		}	
		return depts;
	}
	public List<Dept> selectListChoose(Dept dept){
		SqlSession session = null;
		List<Dept> depts = null;
		
		try {
			session=MyBatisUtil.getSession();
			//方法的第一个参数为DeptMapper.xml文件的命名空间+id
			depts = session.selectList("com.dubito.entity.DeptMapper.selectListChoose",dept);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
			
		}finally{
			MyBatisUtil.closeSession();
		}	
		return depts;
	}
	public int updateSet(Dept dept){
		SqlSession session = null;
		int i = 0;
		
		try {
			session=MyBatisUtil.getSession();
			//方法的第一个参数为DeptMapper.xml文件的命名空间+id
			i = session.update("com.dubito.entity.DeptMapper.updateSet",dept);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
			
		}finally{
			MyBatisUtil.closeSession();
		}	
		return i;
	}
	public List<Dept> selectListForeach(Integer[] ids){
		SqlSession session = null;
		List<Dept> depts = null;
		
		try {
			session=MyBatisUtil.getSession();
			//方法的第一个参数为DeptMapper.xml文件的命名空间+id
			depts = session.selectList("com.dubito.entity.DeptMapper.selectListForeach",ids);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
			
		}finally{
			MyBatisUtil.closeSession();
		}	
		return depts;
	}
}
